home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / scan.test < prev    next >
Encoding:
Text File  |  1995-02-21  |  9.4 KB  |  262 lines

  1. # Commands covered:  scan
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1994 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) scan.test 1.21 95/02/20 16:47:50
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. test scan-1.1 {integer scanning} {
  18.     set a {}; set b {}; set c {}; set d {}
  19.     list [scan "-20 1476 \n33 0" "%d %d %d %d" a b c d] $a $b $c $d
  20. } {4 -20 1476 33 0}
  21. test scan-1.2 {integer scanning} {
  22.     set a {}; set b {}; set c {}
  23.     list [scan "-45 16 7890 +10" "%2d %*d %10d %d" a b c] $a $b $c
  24. } {3 -4 16 7890}
  25. test scan-1.3 {integer scanning} {
  26.     set a {}; set b {}; set c {}; set d {}
  27.     list [scan "-45 16 +10 987" "%ld %d %ld %d" a b c d] $a $b $c $d
  28. } {4 -45 16 10 987}
  29. test scan-1.4 {integer scanning} {
  30.     set a {}; set b {}; set c {}; set d {}
  31.     list [scan "14 1ab 62 10" "%d %x %lo %x" a b c d] $a $b $c $d
  32. } {4 14 427 50 16}
  33. test scan-1.5 {integer scanning} {
  34.     set a {}; set b {}; set c {}; set d {}
  35.     list [scan "12345670 1234567890ab cdefg" "%o     %o %x %lx" a b c d] \
  36.         $a $b $c $d
  37. } {4 2739128 342391 561323 52719}
  38. test scan-1.6 {integer scanning} {
  39.     set a {}; set b {}; set c {}; set d {}
  40.     list [scan "ab123-24642" "%2x %3x %3o %2o" a b c d] $a $b $c $d
  41. } {4 171 291 -20 52}
  42. test scan-1.7 {integer scanning} {
  43.     set a {}; set b {}
  44.     list [scan "1234567 234 567  " "%*3x %x %*o %4o" a b] $a $b
  45. } {2 17767 375}
  46. test scan-1.8 {integer scanning} {
  47.     set a {}; set b {}
  48.     list [scan "a    1234" "%d %d" a b] $a $b
  49. } {0 {} {}}
  50. test scan-1.9 {integer scanning} {
  51.     set a {}; set b {}; set c {}; set d {};
  52.     list [scan "12345678" "%2d %2d %2ld %2d" a b c d] $a $b $c $d
  53. } {4 12 34 56 78}
  54. test scan-1.10 {integer scanning} {
  55.     set a {}; set b {}; set c {}; set d {}
  56.     list [scan "1 2 " "%hd %d %d %d" a b c d] $a $b $c $d
  57. } {2 1 2 {} {}}
  58. if $doNonPortableTests {
  59.     test scan-1.11 {integer scanning} {
  60.     set a {}; set b {};
  61.     list [scan "4294967280 4294967280" "%u %d" a b] $a $b
  62.     } {2 4294967280 -16}
  63. }
  64.  
  65. test scan-2.1 {floating-point scanning} {
  66.     set a {}; set b {}; set c {}; set d {}
  67.     list [scan "2.1 -3.0e8 .99962 a" "%f%g%e%f" a b c d] $a $b $c $d
  68. } {3 2.1 -3e+08 0.99962 {}}
  69. test scan-2.2 {floating-point scanning} {
  70.     set a {}; set b {}; set c {}; set d {}
  71.     list [scan "-1.2345 +8.2 9" "%3e %3lf %f %f" a b c d] $a $b $c $d
  72. } {4 -1.0 234.0 5.0 8.2}
  73. test scan-2.3 {floating-point scanning} {
  74.     set a {}; set b {}; set c {}
  75.     list [scan "1e00004 332E-4 3e+4" "%Lf %*2e %f %f" a b c] $a $c
  76. } {3 10000.0 30000.0}
  77. if $doNonPortableTests {
  78.     test scan-2.4 {floating-point scanning} {
  79.     set a {}; set b {}; set c {}
  80.     list [scan "1. 47.6 2.e2 3.e-" "%f %*f %f %f" a b c] $a $b $c
  81.     } {3 1.0 200.0 3.0}
  82. }
  83. test scan-2.5 {floating-point scanning} {
  84.     set a {}; set b {}; set c {}; set d {}
  85.     list [scan "4.6 99999.7 876.43e-1 118" "%f %f %f %e" a b c d] $a $b $c $d
  86. } {4 4.6 99999.7 87.643 118.0}
  87. test scan-2.6 {floating-point scanning} {
  88.     set a {}; set b {}; set c {}; set d {}
  89.     list [scan "1.2345 697.0e-3 124 .00005" "%f %e %f %e" a b c d] $a $b $c $d
  90. } {4 1.2345 0.697 124.0 5e-05}
  91. test scan-2.7 {floating-point scanning} {
  92.     set a {}; set b {}; set c {}; set d {}
  93.     list [scan "4.6abc" "%f %f %f %f" a b c d] $a $b $c $d
  94. } {1 4.6 {} {} {}}
  95. test scan-2.8 {floating-point scanning} {
  96.     set a {}; set b {}; set c {}; set d {}
  97.     list [scan "4.6 5.2" "%f %f %f %f" a b c d] $a $b $c $d
  98. } {2 4.6 5.2 {} {}}
  99.  
  100. test scan-3.1 {string and character scanning} {
  101.     set a {}; set b {}; set c {}; set d {}
  102.     list [scan "abc defghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d
  103. } {4 abc def ghijk dum}
  104. test scan-3.2 {string and character scanning} {
  105.     set a {}; set b {}; set c {}; set d {}
  106.     list [scan "a       bcdef" "%c%c%1s %s" a b c d] $a $b $c $d
  107. } {4 97 32 b cdef}
  108. test scan-3.3 {string and character scanning} {
  109.     set a {}; set b {}; set c {}
  110.     list [scan "123456 test " "%*c%*s %s %s %s" a b c] $a $b $c
  111. } {1 test {} {}}
  112. test scan-3.4 {string and character scanning} {
  113.     set a {}; set b {}; set c {}; set d
  114.     list [scan "ababcd01234  f 123450" {%4[abcd] %4[abcd] %[^abcdef] %[^0]} a b c d] $a $b $c $d
  115. } {4 abab cd {01234  } {f 12345}}
  116. test scan-3.5 {string and character scanning} {
  117.     set a {}; set b {}; set c {}
  118.     list [scan "aaaaaabc aaabcdefg  + +  XYZQR" {%*4[a] %s %*4[a]%s%*4[ +]%c} a b c] $a $b $c
  119. } {3 aabc bcdefg 43}
  120.  
  121. test scan-4.1 {error conditions} {
  122.     catch {scan a}
  123. } 1
  124. test scan-4.2 {error conditions} {
  125.     catch {scan a} msg
  126.     set msg
  127. } {wrong # args: should be "scan string format ?varName varName ...?"}
  128. test scan-4.3 {error conditions} {
  129.     catch {scan "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21}
  130. } 1
  131. test scan-4.4 {error conditions} {
  132.     catch {scan "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21} msg
  133.     set msg
  134. } {too many fields to scan}
  135. test scan-4.5 {error conditions} {
  136.     list [catch {scan a %D} msg] $msg
  137. } {1 {bad scan conversion character "D"}}
  138. test scan-4.6 {error conditions} {
  139.     list [catch {scan a %O} msg] $msg
  140. } {1 {bad scan conversion character "O"}}
  141. test scan-4.7 {error conditions} {
  142.     list [catch {scan a %X} msg] $msg
  143. } {1 {bad scan conversion character "X"}}
  144. test scan-4.8 {error conditions} {
  145.     list [catch {scan a %F} msg] $msg
  146. } {1 {bad scan conversion character "F"}}
  147. test scan-4.9 {error conditions} {
  148.     list [catch {scan a %E} msg] $msg
  149. } {1 {bad scan conversion character "E"}}
  150. test scan-4.10 {error conditions} {
  151.     list [catch {scan a "%d %d" a} msg] $msg
  152. } {1 {different numbers of variable names and field specifiers}}
  153. test scan-4.11 {error conditions} {
  154.     list [catch {scan a "%d %d" a b c} msg] $msg
  155. } {1 {different numbers of variable names and field specifiers}}
  156. test scan-4.12 {error conditions} {
  157.     set a {}; set b {}; set c {}; set d {}
  158.     list [expr {[scan "  a" " a %d %d %d %d" a b c d] <= 0}] $a $b $c $d
  159. } {1 {} {} {} {}}
  160. test scan-4.13 {error conditions} {
  161.     set a {}; set b {}; set c {}; set d {}
  162.     list [scan "1 2" "%d %d %d %d" a b c d] $a $b $c $d
  163. } {2 1 2 {} {}}
  164. test scan-4.14 {error conditions} {
  165.     catch {unset a}
  166.     set a(0) 44
  167.     list [catch {scan 44 %d a} msg] $msg
  168. } {1 {couldn't set variable "a"}}
  169. test scan-4.15 {error conditions} {
  170.     catch {unset a}
  171.     set a(0) 44
  172.     list [catch {scan 44 %c a} msg] $msg
  173. } {1 {couldn't set variable "a"}}
  174. test scan-4.16 {error conditions} {
  175.     catch {unset a}
  176.     set a(0) 44
  177.     list [catch {scan 44 %s a} msg] $msg
  178. } {1 {couldn't set variable "a"}}
  179. test scan-4.17 {error conditions} {
  180.     catch {unset a}
  181.     set a(0) 44
  182.     list [catch {scan 44 %f a} msg] $msg
  183. } {1 {couldn't set variable "a"}}
  184. test scan-4.18 {error conditions} {
  185.     catch {unset a}
  186.     set a(0) 44
  187.     list [catch {scan 44 %f a} msg] $msg
  188. } {1 {couldn't set variable "a"}}
  189. catch {unset a}
  190. test scan-4.19 {error conditions} {
  191.     list [catch {scan 44 %2c a} msg] $msg
  192. } {1 {field width may not be specified in %c conversion}}
  193. test scan-4.20 {error conditions} {
  194.     list [catch {scan abc {%[}} msg] $msg
  195. } {1 {unmatched [ in format string}}
  196.  
  197. test scan-5.1 {lots of arguments} {
  198.     scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20
  199. } 20
  200. test scan-5.2 {lots of arguments} {
  201.     scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20
  202.     set a20
  203. } 200
  204.  
  205. test scan-6.1 {miscellaneous tests} {
  206.     set a {}
  207.     list [scan ab16c ab%dc a] $a
  208. } {1 16}
  209. test scan-6.2 {miscellaneous tests} {
  210.     set a {}
  211.     list [scan ax16c ab%dc a] $a
  212. } {0 {}}
  213. test scan-6.3 {miscellaneous tests} {
  214.     set a {}
  215.     list [catch {scan ab%c114 ab%%c%d a} msg] $msg $a
  216. } {0 1 114}
  217. test scan-6.4 {miscellaneous tests} {
  218.     set a {}
  219.     list [catch {scan ab%c14 ab%%c%d a} msg] $msg $a
  220. } {0 1 14}
  221. test scan-6.5 {miscellaneous tests} {
  222.     catch {unset tcl_precision}
  223.     set a {}
  224.     scan 1.111122223333 %f a
  225.     set a
  226. } {1.11112}
  227. test scan-6.6 {miscellaneous tests} {
  228.     set tcl_precision 10
  229.     set a {}
  230.     scan 1.111122223333 %lf a
  231.     unset tcl_precision
  232.     set a
  233. } {1.111122223}
  234. test scan-6.7 {miscellaneous tests} {
  235.     set tcl_precision 10
  236.     set a {}
  237.     scan 1.111122223333 %f a
  238.     unset tcl_precision
  239.     set a
  240. } {1.111122223}
  241.  
  242. test scan-7.1 {alignment in results array (TCL_ALIGN)} {
  243.     scan "123 13.6" "%s %f" a b
  244.     set b
  245. } 13.6
  246. test scan-7.2 {alignment in results array (TCL_ALIGN)} {
  247.     scan "1234567 13.6" "%s %f" a b
  248.     set b
  249. } 13.6
  250. test scan-7.3 {alignment in results array (TCL_ALIGN)} {
  251.     scan "12345678901 13.6" "%s %f" a b
  252.     set b
  253. } 13.6
  254. test scan-7.4 {alignment in results array (TCL_ALIGN)} {
  255.     scan "123456789012345 13.6" "%s %f" a b
  256.     set b
  257. } 13.6
  258. test scan-7.5 {alignment in results array (TCL_ALIGN)} {
  259.     scan "1234567890123456789 13.6" "%s %f" a b
  260.     set b
  261. } 13.6
  262.